/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.editor.pipe.ui;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Stroke;

import twaver.TUIManager;
import twaver.network.TNetwork;
import twaver.network.ui.ResizableNodeUI;
import demo.editor.pipe.element.SquarePipe;

public class SquarePipeUI extends ResizableNodeUI {
	
	private SquarePipe squarePipe;

	public SquarePipeUI(TNetwork network, SquarePipe squarePipe) {
		super(network, squarePipe);
		this.squarePipe = squarePipe;
	}
	
	protected void paintCustomDraw(Graphics2D g2d){
		super.paintCustomDraw(g2d);

        int count = squarePipe.getAllCellCount();
        if(count <= 0){
        	return;
        }
		Stroke stroke = TUIManager.getStrokeByType(squarePipe.getInnerStroke());
        if(stroke == null){
        	return;
        }
		
		Object oldHinds = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Stroke oldStroke = g2d.getStroke();

        g2d.setStroke(stroke);
        g2d.setColor(squarePipe.getInnerColor());

        for(int i=0; i<count; i++){
        	Rectangle rect = squarePipe.getPipeHoleBoundsByHoleIndex(i);
        	if(rect != null){
        		g2d.drawRect(rect.x, rect.y, rect.width, rect.height);
        	}
        }
        
        g2d.setStroke(oldStroke);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldHinds);
	}
}